package com.king.cms.upload;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.InstantiationException; 

import com.king.cms.action.BaseDispatchAction;
import com.king.cms.common.FileDoc;
import com.king.cms.common.FileRw;
import com.king.cms.common.FileUp;
import com.king.cms.common.InitSystem;
import com.king.cms.log.LogService;
import com.king.cms.util.page.JpegTool;
 

public class UploadAction extends BaseDispatchAction{
	private static final Log  log = LogFactory.getLog(UploadAction.class);
	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws ServletException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws java.lang.InstantiationException 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws ServletException {
		try{
			
			String filePath = null;
			String fileName = null; 
			FileUp fileUp = new FileUp(request); 
			String fileId = fileUp.getFieldValue("fileId");  
			String type = fileUp.getFieldValue("type"); 
			List fileList = fileUp.getFileList(); 
			 
				if(fileId == null || "null".equals(fileId) || "".equals(fileId)){  
					if(null != fileList){
						FileRw f = new FileRw();
						for(int i = 0;i<fileList.size();i++){
							FileDoc file = (FileDoc)fileList.get(i);
							fileName = file.getFileName();
							String fileSaveName = FileRw.radomFileName(file.getFileName());
							f.saveTempFile(file.getContent(), request.getRealPath("/")+FileRw.getFileSavePath(""),fileSaveName );
							
							String fileFullPath = "http://"+InitSystem.getBpmParam("ip")+":"+InitSystem.getBpmParam("port")+request.getContextPath()+f.getFileSavePath("")+fileSaveName;
							 
							filePath = fileFullPath;
							if("0".equals(type))
								this.getDBOperator(request).update(InitSystem.getBpmParam("file.manage.insert"), 
										   new Object[]{file.getFileName(),fileFullPath,file.getSize()+"",file.getContent()});
							else
								this.getDBOperator(request).update(InitSystem.getBpmParam("file.manage.insert"), 
										   new Object[]{file.getFileName(),fileFullPath,file.getSize()+"",null});
							List list = this.getDBOperator(request).select(InitSystem.getBpmParam("global.select_sql"), null);
							if(list != null){
								for(int j = 0;j<list.size();j++){
									Map m = (Map)list.get(j);
									fileId = m.get("last_insert_id()") + ""; 
								}
							}
						} 
					}
				}else{ 
					 
					if(null != fileList){
						FileRw f = new FileRw();
						for(int i = 0;i<fileList.size();i++){
							FileDoc file = (FileDoc)fileList.get(i);
							fileName = file.getFileName();
							String fileSaveName = FileRw.radomFileName(file.getFileName());
							f.saveTempFile(file.getContent(), request.getRealPath("/")+FileRw.getFileSavePath(""), fileSaveName);
							String fileFullPath = "http://"+InitSystem.getBpmParam("ip")+":"+InitSystem.getBpmParam("port")+request.getContextPath()+f.getFileSavePath("")+fileSaveName;
							filePath = fileFullPath;
							if("0".equals(type)){
								this.getDBOperator(request).update(InitSystem.getBpmParam("file.manage.update"), 
										   new Object[]{file.getFileName(),fileFullPath,file.getSize()+"",file.getContent(),fileId});
								
							}else{
								this.getDBOperator(request).update(InitSystem.getBpmParam("file.manage.update"), 
										   new Object[]{file.getFileName(),fileFullPath,file.getSize()+"",null,fileId});
							}
								
						} 
					} 
				} 
				request.setAttribute("filePath", filePath); ;
				request.setAttribute("fileId", fileId);
	 			request.setAttribute("fileName", fileName);
			   
				 return mapping.findForward("upload");
			 
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	 
	}
}
